/*
 * Winamp.cpp
 *
 * Copyright (c) 2009-2011 [BRN] aka. SWW13.
 *
 * This file is part of [BRN]Lib.
 *
 * [BRN]Lib is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * [BRN]Lib is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with [BRN]Lib.  If not, see <http ://www.gnu.org/licenses/>.
 */

#include "Winamp.h"

namespace BRNlib
{
	namespace Core
	{
		char Winamp::itsCurrentTrack[2048];
		char* Winamp::p;

		HWND Winamp::WinampHandle()
		{
			 return FindWindowA(WINAMP_WINDOW_HANDLE, NULL);
		}

		void Winamp::WCommand(int command)
		{
			SendMessage(Winamp::WinampHandle(), WM_COMMAND, command, 1);
		}
		int Winamp::UCommand(int command, int input)
		{
			return (int)SendMessage(Winamp::WinampHandle(), WM_USER, input, command);
		}

		void Winamp::PlayOrPause()
		{
			Winamp::WCommand(WINAMP_PLAY_OR_PAUSE);
		}
		void Winamp::Next()
		{
			Winamp::WCommand(WINAMP_NEXT_TRACK);
		}
		void Winamp::Previous()
		{
			Winamp::WCommand(WINAMP_PREVIOUS_TRACK);
		}
		void Winamp::Close()
		{
			Winamp::WCommand(WINAMP_CLOSE);
		}
		void Winamp::Stop()
		{
			Winamp::WCommand(WINAMP_STOP);
		}
		void Winamp::ToggleRepeat()
		{
			Winamp::WCommand(WINAMP_TOGGLE_REPEAT);
		}
		void Winamp::ToggleShuffle()
		{
			Winamp::WCommand(WINAMP_TOGGLE_SHUFFLE);
		}
		void Winamp::VolumeUp()
		{
			Winamp::WCommand(WINAMP_RAISE_VOLUME);
		}
		void Winamp::VolumeDown()
		{
			Winamp::WCommand(WINAMP_LOWER_VOLUME);
		}
		void Winamp::Start()
		{
			Winamp::WCommand(WINAMP_START);
		}
		void Winamp::SetVolume(int Volume)
		{
			Winamp::UCommand(WINAMP_CUSTOM_VOLUME, Volume);
		}
		void Winamp::FastForward()
		{
			Winamp::WCommand(WINAMP_FAST_FORWARD);
		}
		void Winamp::FastRewind()
		{
			Winamp::WCommand(WINAMP_FAST_REWIND);
		}

		int Winamp::GetCurrentTime()
		{
			return UCommand(WINAMP_GET_OUTPUTTIME, 0) / 1000;
		}
		int Winamp::GetTotalTime()
		{
			return UCommand(WINAMP_GET_OUTPUTTIME, 1);
		}
		int Winamp::GetPlayedPercent()
		{
			float CurrentTime = GetCurrentTime();
			float TotalTime = GetTotalTime();

			if( TotalTime == 0 || CurrentTime == 0 )
				return 0;

			float Percent = CurrentTime / TotalTime;
			Percent *= 100.0f;

			return (int)Percent;
		}
		bool Winamp::GetShuffle()
		{
			return (UCommand(WINAMP_GET_SHUFFLE, 0) == 0) ? (false) : (true);
		}
		bool Winamp::GetRepeat()
		{
			return (UCommand(WINAMP_GET_REPEAT, 0) == 0) ? (false) : (true);
		}
		int Winamp::GetPlayState()
		{
			return UCommand(WINAMP_ISPLAYING, 0);
		}
		int Winamp::GetVolume()
		{
			return UCommand(WINAMP_CB_MISC, WINAMP_CB_MISC_VOLUME);
		}
		char * Winamp::CurrentTrack()
		{
			GetWindowTextA(Winamp::WinampHandle(), itsCurrentTrack, sizeof(itsCurrentTrack));
			p = itsCurrentTrack + strlen(itsCurrentTrack) - 8;
			
			while (p >= itsCurrentTrack)
			{
				if (!strnicmp(p, "- Winamp", 8)) 
				{
					break;
					p--;
				}
			}
			
			if (p >= itsCurrentTrack) 
				p--;
			
			while (p >= itsCurrentTrack && *p == ' ')
			{ 
				p--;
				*++p = 0;
			}

			return itsCurrentTrack;
		}
	}
}